<template>
	<view>
		<view class="head">
			<view class="head_left" @tap="back()">
				<image src="../../../static/wode/return.png" mode=""></image>
				<text>返回</text>
			</view>
			<view class="head_mid">
				学校经历
			</view>
			<view class="head_right" @tap="delData" v-if="recordId!=''">
				删除
			</view>
			<view class="head_right" v-else ></view>
		</view>
		<view class="bg">

		</view>
		<view class="body">
			<view class="body_item addBorder">
				<text>学校名称</text>
				<input class="marginLeft" type="text" value="" v-model="schoolName" placeholder="请输入学校名称" />
			</view>
			<view class="body_item">
				<text>时间段</text>
				<!-- <input type="text" value="" placeholder="格式:2012.09-2016.06" /> -->
				<view class="marginLeft flexBox">
					<picker class="fontStyle" mode="date" fields="month" start="1971-01-01" :end="endDate" @change="starDateChange">
						<view class="uni-input" v-if="starDate=='请选择'">请选择</view>
						<view class="uni-input" v-else>{{starDate}}</view>
					</picker>
					<text style="padding-left: 10upx;">--</text>
					<picker class="fontStyle rightText" fields="month" mode="date" start="1971-01-01" :end="endDate" @change="closeDateChange">
						<view class="uni-input" v-if="closeDate=='请选择'">请选择</view>
						<view class="uni-input" v-else>{{closeDate}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="btn" @tap="preservation">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				schoolName: '', //学校名称
				starDate: '请选择',
				closeDate: "请选择",
				endDate: "",
				user_id: "",
				recordId: '',
			};
		},
		onLoad(option) {
			this.user_id = option.user_id
			if (option.recordId != undefined) {
				this.recordId = option.recordId
				this.selectSchoolInfor()
			}
		},
		onShow() {
			var date = new Date().getTime();
			this.endDate = this.timeFromDate(date)
			console.log(this.endDate)

		},
		methods: {
			back() { //返回
				uni.navigateBack({
					delta: 1
				})
			},
			starDateChange: function(e) {
				this.starDate = e.target.value
			},
			closeDateChange: function(e) {
				this.closeDate = e.target.value
			},
			preservation() {
				if (this.recordId != '' && this.recordId != undefined) { //修改学校经历
					var data = {
						id: this.recordId,
						user_id: this.user_id,
						school_name: this.schoolName,
						start_time: this.starDate,
						end_time: this.closeDate
					};
					var me = this;
					this.myAjaxNewPost({
						model: 'user',
						controllerName: 'userSchool',
						actionName: 'update',
						data: data,
						successBack: function(res) {
							console.log(res);
							// this.user_Info=res.data[0]
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
							if (res.data.code == 0) {
								uni.navigateBack({
									delta: 1
								})
							}
						},
					})
				} else { //添加经历
					if (this.schoolName == '' || this.starDate == '请选择' || this.closeDate == '请选择') {
						uni.showToast({
							title: '请填写完整信息',
							icon: 'none'
						})
					} else {
						var data = {
							user_id: this.user_id,
							school_name: this.schoolName,
							start_time: this.starDate,
							end_time: this.closeDate
						};
						var me = this;
						this.myAjaxNewPost({
							model: 'user',
							controllerName: 'userSchool',
							actionName: 'insert',
							data: data,
							successBack: function(res) {
								console.log(res);
								// this.user_Info=res.data[0]
								uni.showToast({
									title: res.data.message,
									icon: 'none'
								})
								if (res.data.code == 0) {
									uni.navigateBack({
										delta: 1
									})
								}
							},
						})
					}
				}
			},
			delData() {
				if (this.recordId != '') {
					var me = this;
					var data = {
						id: this.recordId
					}
					var me = this;
					this.myAjaxNew({
						model: 'user',
						controllerName: 'userSchool',
						actionName: 'delete',
						data: data,
						successBack: function(res) {
							console.log(res);
							if (res.data.code == 0) {
								uni.showToast({
									title: '删除成功',
									icon: 'none'
								})
								uni.navigateBack({
									delta: 1
								})
							}
						}
					})
				}
			},
			selectSchoolInfor() { //查询学校经历
				var data = {
					user_id: this.user_id,
					id: this.recordId
				};
				var me = this;
				this.myAjaxNew({
					model: 'user',
					controllerName: 'userSchool',
					actionName: 'query',
					data: data,
					successBack: function(res) {
						console.log(res);
						// this.user_Info=res.data[0]
						// 						if (res.data.code == 0) {
						// 							if (res.data.data.length!=0) {
						// 								uni.showToast({
						// 									title: res.data.message,
						// 									icon: 'none'
						// 								})
						// for (var i = 0; i < res.data.data.length; i++) { //取第一条记录
						me.schoolName = res.data.data[0].schoolName
						me.recordId = res.data.data[0].id //记录id
						me.starDate = res.data.data[0].start_time
						me.closeDate = res.data.data[0].end_time
						// 								}
						// // 							} 
						// 						}
					},
				})
			}
		},
	}
</script>

<style scoped>
	.head {
		height: 128upx;
		display: flex;
		justify-content: space-between;
		font-size: 32upx;
		padding: 0 24upx;
		background: #ffffff;
	}

	.head image {
		height: 36upx;
		width: 20upx;
		margin-right: 9upx;
	}

	.head_left {
		display: flex;
		align-items: center;
		color: #666666;
	}

	.head_mid {
		font-size: 36upx;
		color: #333333;
	}
	.head_right{
		width: 150upx;
	}

	.body {
		margin-top: 20upx;
		background: #ffffff;
	}

	.body_item {
		color: #333333;
		font-size: 32upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 24upx 24upx;
	}

	.marginLeft {
		position: absolute;
		left: 280upx;
	}

	.flexBox {
		display: flex;
		align-items: center;
	}

	.rightText {
		margin-left: 15upx;
	}

	.addBorder {
		border-bottom: 1upx solid #E6E6E6;
	}

	.body_item text {
		color: #999999;
	}

	.btn {
		position: absolute;
		bottom: 60upx;
		left: 24upx;
	}
</style>
